/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BL;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DarkIaspis
 */
public class CaesarCipher {

    public static final double[] englishCharsFrequency = {0.080000, 0.015000,
        0.030000, 0.040000, 0.130000, 0.020000, 0.015000, 0.060000, 0.065000,
        0.005000, 0.005000, 0.035000, 0.030000, 0.070000, 0.080000, 0.020000,
        0.002000, 0.065000, 0.060000, 0.090000, 0.030000, 0.010000, 0.015000,
        0.005000, 0.020000, 0.002000};

    public static String encrypt(String text, int key) {
        key = key % 26;
        text = text.toUpperCase();
        char[] c = new char[text.length()];
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) < 'A' || text.charAt(i) > 'z' || (text.charAt(i) < 'a' && text.charAt(i) > 'Z')) {
                c[i] = text.charAt(i);
            } else {
                c[i] = (char) ((text.charAt(i) + key - 'A') % 26 + 'A');
            }
        }
        return new String(c);
    }

    public static String decrypt(String text, int key) {
        key = key % 26;
        return encrypt(text, 26 - key);
    }

    public static int[] getMostProbablyKeys(String ciphertext, int keys) {
        ciphertext = ciphertext.toUpperCase();
        double[] ciphertextCharsFrequency = new double[26];
        double[] correlation = new double[26];
        int[] textAsInteger = new int[ciphertext.length() + 1];
        int chars = 0, i = 0;
        while (i < ciphertext.length()) {
            if ((ciphertext.charAt(i) >= 'A' && ciphertext.charAt(i) <= 'Z') || (ciphertext.charAt(i) >= 'a' && ciphertext.charAt(i) <= 'z')) {
                textAsInteger[chars] = ciphertext.charAt(i) - 'A';
                ciphertextCharsFrequency[textAsInteger[chars]]++;
                chars++;
            }
            i++;
        }

        for (i = 0; i < 26; i++) {
            ciphertextCharsFrequency[i] = ciphertextCharsFrequency[i] / (chars);
            System.out.println((char) ('A' + i) + "-->" + ciphertextCharsFrequency[i]);
        }
        double[] ecf = new double[26];
        for (i = 0; i < 26; i++) {
            for (int j = 0; j < 26; j++) {
                ecf[j] = englishCharsFrequency[(26 + j - i) % 26];
            }
            try {
                correlation[i] = ScalarProduct.perform(ciphertextCharsFrequency, ecf);
            } catch (WrongSizeException ex) {
                Logger.getLogger(CaesarCipher.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        double app;
        int[] indexes = new int[java.lang.Math.min(26, keys)];
        for (i = 0; i < java.lang.Math.min(26, keys); i++) {
            app=-1;
            for (int j = 0; j < 26; j++) {
                if (correlation[j]>app) {
                    app = correlation[j];
                    indexes[i] = j;
                }
            }
            correlation[indexes[i]] = -1;
        }
        return indexes;
    }
}
